<?php

/**
 * MyProjectNameHere <http://www.example.com>
 * Model Class
 *
 * It is recommended to extend Model classes from WWW_Factory in order to 
 * provide various useful functions and API access for the Model.
 *
 * @package    Factory
 * @author     DeveloperNameHere <email@example.com>
 * @copyright  Copyright (c) 2012, ProjectOwnerNameHere
 * @license    Unrestricted
 * @tutorial   /doc/pages/guide_mvc.htm
 * @since      1.0.0
 * @version    1.0.0
 */

class WWW_model_classifieds extends WWW_Factory {

	/**
	 * It is recommended to define all data variables here. Usually the 
	 * data variables have the same names as the column names of database 
	 * rows from a table.
	 */	
	public $conn;
	/**
	 * Alternative to __construct()
	 *
	 * WWW_Factory does not allow to overwrite __construct() method, so 
	 * this __initialize() is used instead and loaded automatically when 
	 * object is created.
	 *
	 * @return boolean
	 */
	public function __initialize(){
		require('engine/class.www-database.php');
		$this->conn=new WWW_Database();				
		// Do something here
		return true;
		
	}
	
	/**
	 * This is intended to load data from database
	 *
	 * @param integer $id identifier of the object loaded
	 * @return boolean
	 */
	
	public function create_category($data){
	$query="insert into `classifieds_category` (category,description,status) VALUES ('".$data['category']."','".$data['description']."','1')";
	$result=$this->conn->dbCommand($query);		
	
	//print_r($query);exit;
	
		if(!isset($data['category']) || $data['category']=='')
		{
			return array('message'=>'Empty Category ');
		}
	   elseif( strlen($data['description'])>200)
		{
			return array('message'=>'Description exceeds 200 characters ');
		}
		elseif($result){
			return array('message'=>'Successfully Created');			
		}
		else{
			return array('message'=>'Create Failed');
		}
	}
		public function create_subcategory($data){
	$query="insert into `classifieds_subcategory` (subcategory,description,status) VALUES ('".$data['category']."','".$data['description']."','1')";
	$result=$this->conn->dbCommand($query);		
	
	//print_r($query);exit;
	
		if(!isset($data['subcategory']) || $data['subcategory']=='')
		{
			return array('message'=>'Empty Category ');
		}
	   elseif( strlen($data['description'])>200)
		{
			return array('message'=>'Description exceeds 200 characters ');
		}
		elseif($result){
			return array('message'=>'Successfully Created');			
		}
		else{
			return array('message'=>'Create Failed');
		}
	}
	public function update_category($data){
		$result=$this->conn->dbCommand("update classifieds_category set category='".$data['category']."',description='".$data['description']."' where id=".$data['id']);		
		if(!isset($data['category']) || $data['category']=='')
		{
			return array('message'=>'Empty Category ');
		}
	   elseif( strlen($data['description'])>200)
		{
			return array('message'=>'Description exceeds 200 characters ');
		}
		elseif($result){
			return array('message'=>'Updated Successfully');			
		}else{
			return array('message'=>'Updates Failed');
		}	
	
	}
	
		public function update_subcategory($data){
		$result=$this->conn->dbCommand("update classifieds_subcategory set subcategory='".$data['subcategory']."',description='".$data['description']."' where id=".$data['id']);		
		if(!isset($data['subcategory']) || $data['subcategory']=='')
		{
			return array('message'=>'Empty Category ');
		}
	   elseif( strlen($data['description'])>200)
		{
			return array('message'=>'Description exceeds 200 characters ');
		}
		elseif($result){
			return array('message'=>'Updated Successfully');			
		}else{
			return array('message'=>'Updates Failed');
		}	
	
	}
	
	public function all_categories($data){
		$result=$this->conn->dbMultiple("select * from classifieds_category ");
		if($result){
			$totalrows = $this->conn->dbSingle('SELECT FOUND_ROWS() as rows;');
		return array('message'=>"All Categories","info"=>$result,'total'=>$totalrows);
		}
		else
		return array('message'=>'No Categories');
		
	}
	public function get_category(){
		extract($_POST);		
		$result=$this->conn->dbSingle("select *from classifieds_category where id=".$id);
		if($result)
		return array('message'=>"Category Info",'info'=>$result);
		else
		return array('message'=>'No Categories');
	}
	public function create(){
		extract($_POST);		
		$data=$this->conn->dbCommand("INSERT INTO `classifieds` ( `category_id`, `logo`, `description`, `posted_date`, `contact_details`, `phone_number`, `mobile_number`,`email`, `start_date`, `created_by`, `create_date`, `status`) VALUES ('".$category_id."', '".$logo."', '".$description."', '".$posted_date."', '".$contact_details."', '".$phone_number."', '".$mobile_number."','".$email."', '".$start_date."', '".$created_by."',NOW(),  '1')");		
		if($data){
			return array('message'=>'Successfully Created');			
		}else{
			return array('message'=>'Creation Failed');
		}	
	}
	public function update(){
		extract($_POST);
		$data=$this->conn->dbCommand("UPDATE `classifieds` set  `category_id`='".$category_id."', `logo`='".$logo."',`description`='".$description."', `posted_date`='".$posted_date."',`contact_details`='".$contact_details."',`phone_number`='".$phone_number."', `mobile_number`='".$mobile_number."', `email`='".$email."', `start_date`='".$start_date."', `updated_by`='".$updated_by."', `updated_date`=NOW()  where id=".$id);		
		if($data){
			return array('message'=>'Updated Successfully');			
		}else{
			return array('message'=>'Updates Failed');
		}	
	
	}
	public function all(){
		extract($_POST);
		$status = ($status=='')?'':" where status='1'";
		$result=$this->conn->dbMultiple("select * from classifieds".$status);
		if($result){
			$totalrows = $this->conn->dbSingle('SELECT FOUND_ROWS() as rows;');
		return array('message'=>"All classifieds","info"=>$result,'total'=>$totalrows);
		}
		else
		return array('message'=>'No Doctors');
		
	}
	public function get(){
		extract($_POST);		
		$result=$this->conn->dbSingle("select *from classifieds where id=".$id);
		if($result)
		return array('message'=>"classified Info",'info'=>$result);
		else
		return array('message'=>'classified Not Found');
	}
	public function get_category_classifieds(){
		extract($_POST);
		$result=$this->conn->dbMultiple("select * from classifieds where category_id=".$category_id." and status='1'");
		if($result){
			$totalrows = $this->conn->dbSingle('SELECT FOUND_ROWS() as rows');
		return array('message'=>"Category classifieds","info"=>$result,'total'=>$totalrows);
		}
		else
		return array('message'=>'No Classifieds');
	}
	public function disabled_classified(){
		extract($_POST);		
		$statusRequest = ($status=='0')?"Inactive":"Activated";
		$data=$this->conn->dbCommand("UPDATE `classifieds` set status='".$status."' where id=".$id);
		if($data)
			return array('message'=>$statusRequest." Successfully");		
		else
			return array('message'=>$statusRequest.' Fails');
	   	
	}
	public function delete_classified(){
		extract($_POST);
		$data=$this->conn->dbCommand("delete from `classifieds` where id=".$id);
		if($data)
			return array('message'=>"Deleted Successfully");		
		else
			return array('message'=>'Deletion Fails');
	}
}
	
?>